Educe
This crate provides procedural macros to help you implement Rust-built-in traits quickly.
Features
By default, every trait this crate supports will be enabled. You can disable all of them by disabling the default features and enable only the traits that you want to use by adding them to features
explictly.
For example,
[]
= "*"
= ["Debug", "Default", "Hash", "Clone", "Copy"]
= false
Debug
Use #[derive(Educe)]
and #[educe(Debug)]
to implement the Debug
trait for a struct, an enum, or a union. It supports to change the name of your types, variants and fields. You can also ignore some fields, or set a trait and/or a method to replace the Debug
trait used by default. Also, you can even format a struct to a tuple, and vice versa.
Basic Usage
extern crate educe;
Change the Name of a Type, a Variant or a Field
The name
attribute can help you rename a type, a variant or a field.
extern crate educe;
Ignore Fields
The ignore
attribute can ignore specific fields.
extern crate educe;
Fake Structs and Tuples
With the named_field
attribute, structs can be formatted as tuples and tuples can be formatted as structs.
extern crate educe;
Use Another Method or Trait to Do the Format Thing
The trait
and method
attributes can be used to replace the Debug
trait for fields. If you only set the trait
parameter, the method
will be set to fmt
automatically by default.
extern crate educe;
use ;
;
;
Generic Parameters Bound to the Debug
Trait or Others
The #[educe(Debug(bound))]
attribute can be used to add the Debug
trait bound to all generic parameters for the Debug
implementation.
extern crate educe;
Or you can set the where predicates by yourself.
extern crate educe;
use ;
;
;
Union
A union will be formatted to a u8
slice, because we don't know it's field at runtime. The fields of a union cannot be ignored, renamed or formated with other methods or traits.
extern crate educe;
PartialEq
Use #[derive(Educe)]
and #[educe(ParitalEq)]
to implement the ParitalEq
trait for a struct or an enum. It supports to ignore some fields, or set a trait and/or a method to replace the ParitalEq
trait used by default.
Basic Usage
extern crate educe;
Ignore Fields
The ignore
attribute can ignore specific fields.
extern crate educe;
Use Another Method or Trait to Do Comparing
The trait
and method
attributes can be used to replace the PartialEq
trait for fields. If you only set the trait
parameter, the method
will be set to eq
automatically by default.
extern crate educe;
Generic Parameters Bound to the PartialEq
Trait or Others
The #[educe(PartialEq(bound))]
attribute can be used to add the PartialEq
trait bound to all generic parameters for the PartialEq
implementation.
extern crate educe;
Or you can set the where predicates by yourself.
extern crate educe;
Eq
Use #[derive(Educe)]
and #[educe(Eq)]
to implement the Eq
trait for a struct, an enum or a union.
Basic Usage
extern crate educe;
Generic Parameters Bound to the Eq
Trait or Others
The #[educe(Eq(bound))]
attribute can be used to add the Eq
trait bound to all generic parameters for the Eq
implementation.
extern crate educe;
Or you can set the where predicates by yourself. (NOTE: The Eq
trait depends on the PartialEq
(PartialEq<Self>
) trait.)
extern crate educe;
PartialOrd
Use #[derive(Educe)]
and #[educe(PartialOrd)]
to implement the PartialOrd
trait for a struct or an enum. It supports to ignore some fields, or set a trait and/or a method to replace the PartialOrd
trait used by default. The rank of variants and fields can also be modified.
Basic Usage
extern crate educe;
Ignore Fields
The ignore
attribute can ignore specific fields.
extern crate educe;
Use Another Method or Trait to Do Comparing
The trait
and method
attributes can be used to replace the PartialOrd
trait for fields. If you only set the trait
parameter, the method
will be set to partial_cmp
automatically by default.
extern crate educe;
use Ordering;
Generic Parameters Bound to the PartialOrd
Trait or Others
The #[educe(PartialOrd(bound))]
attribute can be used to add the PartialOrd
trait bound to all generic parameters for the PartialOrd
implementation.
extern crate educe;
Or you can set the where predicates by yourself. (NOTE: The PartialOrd
trait depends on the PartialEq
(PartialEq<Self>
) trait.)
extern crate educe;
use Ordering;
Ranking
Each field can add a #[educe(PartialOrd(rank = priority_value))]
attribute where priority_value
is a positive integer value to determine their comparing precedence (lower priority_value
leads to higher priority). The default priority_value
for a field dependends on its ordinal (the lower the front) and is always lower than any custom priority_value
.
extern crate educe;
Each variant can add a #[educe(PartialOrd(rank = comparison_value))]
attribute where comparison_value
is a positive integer value to override the value or the ordinal of a variant for comparison.
extern crate educe;
Ord
Use #[derive(Educe)]
and #[educe(Ord)]
to implement the Ord
trait for a struct or an enum. It supports to ignore some fields, or set a trait and/or a method to replace the Ord
trait used by default. The rank of variants and fields can also be modified.
Basic Usage
extern crate educe;
Ignore Fields
The ignore
attribute can ignore specific fields.
extern crate educe;
Use Another Method or Trait to Do Comparing
The trait
and method
attributes can be used to replace the Ord
trait for fields. If you only set the trait
parameter, the method
will be set to cmp
automatically by default.
extern crate educe;
use Ordering;
Generic Parameters Bound to the Ord
Trait or Others
The #[educe(Ord(bound))]
attribute can be used to add the Ord
trait bound to all generic parameters for the Ord
implementation.
extern crate educe;
Or you can set the where predicates by yourself. (NOTE: The Ord
trait depends on the PartialOrd
(PartialOrd<Self>
) trait and the Eq
trait.)
extern crate educe;
use Ordering;
Ranking
Each field can add a #[educe(Ord(rank = priority_value))]
attribute where priority_value
is a positive integer value to determine their comparing precedence (lower priority_value
leads to higher priority). The default priority_value
for a field dependends on its ordinal (the lower the front) and is always lower than any custom priority_value
.
extern crate educe;
Each variant can add a #[educe(Ord(rank = comparison_value))]
attribute where comparison_value
is a positive integer value to override the value or the ordinal of a variant for comparison.
extern crate educe;
Hash
Use #[derive(Educe)]
and #[educe(Hash)]
to implement the Hash
trait for a struct or an enum. It supports to ignore some fields, or set a trait and/or a method to replace the Hash
trait used by default.
Basic Usage
extern crate educe;
Ignore Fields
The ignore
attribute can ignore specific fields.
extern crate educe;
Use Another Method or Trait to Do Hashing
The trait
and method
attributes can be used to replace the Hash
trait for fields. If you only set the trait
parameter, the method
will be set to hash
automatically by default.
extern crate educe;
use ;
;
;
Generic Parameters Bound to the Hash
Trait or Others
The #[educe(Hash(bound))]
attribute can be used to add the Hash
trait bound to all generic parameters for the Hash
implementation.
extern crate educe;
Or you can set the where predicates by yourself.
extern crate educe;
use ;
;
;
Default
Use #[derive(Educe)]
and #[educe(Default)]
to implement the Default
trait for a struct, an enum, or a union. It supports to set the default value for your type directly, or set the default values for specific fields.
Basic Usage
For enums and unions, you need to assign a variant (of a enum) and a field (of a union) as default unless the number of variants of an enum or the number of fields of a union is exactly one.
extern crate educe;
union Union
The Default Value for the Whole Type
The #[educe(Default(expression = "expression"))]
attribute can be used to set the default value for your type by an expression.
extern crate educe;
union Union
The Default Values for Specific Fields
The #[educe(Default = literal)]
attribute or the #[educe(Default(expression = "expression"))]
attribute can be used to set the default value for a specific field by a literal value or an expression.
extern crate educe;
union Union
Generic Parameters Bound to the Default
Trait or Others
The #[educe(Default(bound))]
attribute can be used to add the Default
trait bound to all generic parameters for the Default
implementation.
extern crate educe;
Or you can set the where predicates by yourself.
extern crate educe;
The new
Associated Function
With the #[educe(Default(new))]
attribute, your type will have an extra associated function called new
. That can be used to invoke the default
method of the Default
trait.
extern crate educe;
Clone
Use #[derive(Educe)]
and #[educe(Clone)]
to implement the Clone
trait for a struct, an enum, or a union. It supports to set a trait and/or a method to replace the Clone
trait used by default.
Basic Usage
extern crate educe;
Use Another Method or Trait to Do Cloning
The trait
and method
attributes can be used to replace the Clone
trait for fields. If you only set the trait
parameter, the method
will be set to clone
automatically by default.
extern crate educe;
Generic Parameters Bound to the Clone
Trait or Others
The #[educe(Clone(bound))]
attribute can be used to add the Clone
trait bound or the Copy
trait bound (if the #[educe(Copy)]
attribute exists) to all generic parameters for the Clone
implementation.
extern crate educe;
Or you can set the where predicates by yourself.
extern crate educe;
Union
The #[educe(Clone)]
attribute can be used for a union which also needs to implement the Copy
trait. The fields of a union cannot be cloned with other methods or traits.
extern crate educe;
union Union
Copy
Use #[derive(Educe)]
and #[educe(Copy)]
to implement the Copy
trait for a struct, an enum, or a union.
Basic Usage
extern crate educe;
Generic Parameters Bound to the Copy
Trait or Others
The #[educe(Copy(bound))]
attribute can be used to add the Copy
trait bound to all generic parameters for the Copy
implementation.
extern crate educe;
Or you can set the where predicates by yourself.
extern crate educe;
Copy and Clone
If you implement both of the Copy
trait and the Clone
trait by Educe, the bound for the Clone
trait needs to include the Copy
trait due to Copy, Clone
optimization.
Deref
Use #[derive(Educe)]
and #[educe(Deref)]
to implement the Deref
trait for a struct or an enum.
Basic Usage
You need to assign a field as a default inmutable dereferencing field unless the number of fields is exactly one.
extern crate educe;
DerefMut
Use #[derive(Educe)]
and #[educe(DerefMut)]
to implement the DerefMut
trait for a struct or an enum.
Basic Usage
You need to assign a field as a default mutable dereferencing field unless the number of fields is exactly one.
extern crate educe;
The mutable dereferencing fields don't need to be the same as the inmutable dereferencing fields. But their type must be the same.
TODO
There is a lot of work to be done. Unimplemented traits are listed below:
From
Into
FromStr
TryFrom
TryInto
Crates.io
https://crates.io/crates/educe